package com.example.terminal.activity;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.terminal.R;
import com.example.terminal.adapter.StocktakingDetailsAdapter;
import com.example.terminal.base.BaseActivity;
import com.example.terminal.bean.CommonBean;
import com.example.terminal.bean.StocktakingDetailsBean;
import com.example.terminal.bean.StocktakingDetailsBean.DataBean;
import com.example.terminal.bean.StocktakingDetailsBean.DataBean.TcheckDeviceListBean;
import com.example.terminal.bean.StoreroomListBean;
import com.example.terminal.bean.TcheckDeviceListDataBean;
import com.example.terminal.bean.UserListBeans;
import com.example.terminal.global.Constant;
import com.example.terminal.http.NetworkUtils;
import com.example.terminal.http.network.OkGoBackListener;
import com.example.terminal.util.CommonUtils;
import com.example.terminal.util.ToastUtils;
import com.example.terminal.view.dialog.StaffDialog;
import com.example.terminal.view.dialog.StoreroomDialog;
import com.example.terminal.view.edit.DeviceListDialogActivity;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lzy.okgo.model.HttpParams;

import java.util.ArrayList;
import java.util.List;

/**
 * 盘点管理-新增页面
 */
public class StocktakingtNewAddActivity extends BaseActivity {
    private TwinklingRefreshLayout mRefreshLayout;
    private RecyclerView mRvContainer;
    private RelativeLayout mRlContentNull;
    private ImageButton mIbBack;
    private Button mBt_StocktakingAddDevice;
    private ImageView mIv_StocktakingStaffDown, mIv_StocktakingStoreroomDown;
    private TextView mTvTitleName, mTv_StocktakingStaff, mTv_StocktakingStoreroom, mTv_Check, mTv_Submit, mTv_SaveDraft;
    private EditText mEt_StocktakingName;
    private RelativeLayout mRl_StocktakingStaff, mRl_StocktakingStoreroom;

    private List<TcheckDeviceListBean> tcheckDeviceList;
    private StocktakingDetailsAdapter stocktakingDetailsAdapter;
    private StocktakingDetailsBean stocktakingDetailsBean;
    private DataBean mDataBean;
    private int mId = 0;
    private int storeHouseId = 0;


    @Override
    protected int setContentView() {
        return R.layout.activity_new_add_stocktakingt;
    }

    @Override
    protected void initView() {
        mRefreshLayout = findViewById(R.id.mRefreshLayout);
        mRvContainer = findViewById(R.id.mRvContainer);
        mRlContentNull = findViewById(R.id.mRlContentNull);

        mIbBack = findViewById(R.id.mIbBack);//返回按键
        mTvTitleName = findViewById(R.id.mTvTitleName);//标题

        mEt_StocktakingName = findViewById(R.id.mEt_StocktakingName);//盘点名称

        mRl_StocktakingStaff = findViewById(R.id.mRl_StocktakingStaff);//盘点人员
        mTv_StocktakingStaff = findViewById(R.id.mTv_StocktakingStaff);//盘点人员
        mIv_StocktakingStaffDown = findViewById(R.id.mIv_StocktakingStaffDown);//盘点人员

        mRl_StocktakingStoreroom = findViewById(R.id.mRl_StocktakingStoreroom);//库房名称
        mTv_StocktakingStoreroom = findViewById(R.id.mTv_StocktakingStoreroom);//库房名称
        mIv_StocktakingStoreroomDown = findViewById(R.id.mIv_StocktakingStoreroomDown);//库房名称

        mBt_StocktakingAddDevice = findViewById(R.id.mBt_StocktakingAddDevice);//添加设备

        mTv_Check = findViewById(R.id.mTv_Check);//核对
        mTv_Submit = findViewById(R.id.mTv_Submit);//提交
        mTv_SaveDraft = findViewById(R.id.mTv_SaveDraft);//保存草稿

        mIbBack.setOnClickListener(this::onClickSort);
        mRl_StocktakingStaff.setOnClickListener(this::onClickSort);
        mRl_StocktakingStoreroom.setOnClickListener(this::onClickSort);
        mBt_StocktakingAddDevice.setOnClickListener(this::onClickSort);
        mTv_Check.setOnClickListener(this::onClickSort);
        mTv_Submit.setOnClickListener(this::onClickSort);
        mTv_SaveDraft.setOnClickListener(this::onClickSort);

        mEt_StocktakingName.addTextChangedListener(new TextWatcher() {
            // 输入前调用
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            // 输入中调用（实时监听）
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            // 输入完成（文本变化后）调用
            @Override
            public void afterTextChanged(Editable s) {
                String string = s.toString();

                if (mDataBean == null)
                    mDataBean = new DataBean();

                mDataBean.setName(string);
            }
        });
    }

    @Override
    protected void initData() {
        mTvTitleName.setText("新增盘点");

        mId = mBundle.getInt(Constant.ID);

    }

    @Override
    protected void initConnect() {
        NetworkUtils.getStocktakingDetails(new OkGoBackListener(mActivity) {
            @Override
            public void onSuccess(Object body) {
                stocktakingDetailsBean = (StocktakingDetailsBean) body;

                mDataBean = stocktakingDetailsBean.getData();

                tcheckDeviceList = stocktakingDetailsBean.getData().getTcheckDeviceList();

                setViewData();

                initAdapter();
            }
        }, mId);
    }

    private void setViewData() {
        if (mDataBean == null)
            return;

        mEt_StocktakingName.setText(mDataBean.getName());


        NetworkUtils.getSystemUserList(new OkGoBackListener((Activity) mActivity) {
            @Override
            public void onSuccess(Object body) {
                List<UserListBeans.RowsBean> rowsBeanList = (List<UserListBeans.RowsBean>) body;

                if (rowsBeanList != null && rowsBeanList.size() > 0) {
                    for (int i = 0; i < rowsBeanList.size(); i++) {
                        if (mDataBean.getCheckUserId().equals(String.valueOf(rowsBeanList.get(i).getUserId()))) {
                            mTv_StocktakingStaff.setText(rowsBeanList.get(i).getNickName());

                        }
                    }
                }

            }
        });

        HttpParams valueParams = new HttpParams();
        valueParams.put(Constant.PAGESIZE, "99999");

        NetworkUtils.getTerminalHouseList(new OkGoBackListener((Activity) mActivity) {
            @Override
            public void onSuccess(Object body) {
                List<StoreroomListBean.RowsBean> rowsBeanList = (List<StoreroomListBean.RowsBean>) body;

                if (rowsBeanList != null && rowsBeanList.size() > 0) {
                    for (int i = 0; i < rowsBeanList.size(); i++) {
                        if (mDataBean.getStoreHouseId() == rowsBeanList.get(i).getId()) {
                            mTv_StocktakingStaff.setText(rowsBeanList.get(i).getNickName());
                            storeHouseId = rowsBeanList.get(i).getId();
                            mTv_StocktakingStoreroom.setText(rowsBeanList.get(i).getName());
                        }
                    }
                }
            }
        }, valueParams);

    }

    private void initAdapter() {
        if (stocktakingDetailsBean == null)
            return;


        mRlContentNull.setVisibility(tcheckDeviceList == null || tcheckDeviceList.size() == 0 ? View.VISIBLE : View.GONE);
        mRefreshLayout.setVisibility(tcheckDeviceList == null || tcheckDeviceList.size() == 0 ? View.GONE : View.VISIBLE);


        if (stocktakingDetailsAdapter == null) {
            stocktakingDetailsAdapter = new StocktakingDetailsAdapter(mActivity, tcheckDeviceList);
            mRvContainer.setAdapter(stocktakingDetailsAdapter);
            mRvContainer.setLayoutManager(new LinearLayoutManager(mActivity));

//            stocktakingDetailsAdapter.setOnItemClickListener(new DetailsItemClickListener());
        } else {
            stocktakingDetailsAdapter.updateAdapter(tcheckDeviceList);
        }
    }

    public void onClickSort(View view) {
        if (mDataBean == null)
            mDataBean = new DataBean();

        switch (view.getId()) {
            case R.id.mIbBack://返回
                onFinish();
                break;
            case R.id.mRl_StocktakingStaff://盘点人员
                StaffDialog.getInstance(mActivity)
                        .setOnItemClickListener(new StaffDialog.OnSheetItemClickListener() {
                            @Override
                            public void onClick(UserListBeans.RowsBean rowsBean) {
                                if (rowsBean != null) {
                                    mDataBean.setCheckUserId(String.valueOf(rowsBean.getUserId()));
                                    mTv_StocktakingStaff.setText(rowsBean.getNickName());
                                }
                            }
                        }).show();
                break;
            case R.id.mRl_StocktakingStoreroom://库房名称
                StoreroomDialog.getInstance(mActivity)
                        .setOnItemClickListener(new StoreroomDialog.OnSheetItemClickListener() {
                            @Override
                            public void onClick(StoreroomListBean.RowsBean rowsBean) {
                                if (rowsBean != null) {
                                    storeHouseId = rowsBean.getId();
                                    mDataBean.setStoreHouseId(rowsBean.getId());
                                    mTv_StocktakingStoreroom.setText(rowsBean.getName());
                                }
                            }
                        }).show();
                break;
            case R.id.mBt_StocktakingAddDevice://添加设备
                String mStocktakingStoreroom = mTv_StocktakingStoreroom.getText().toString();
                if (mStocktakingStoreroom.isEmpty()) {
                    ToastUtils.showToast("请选择库房名称");
                    return;
                }

                DeviceListDialogActivity.openActivity(mActivity, new DeviceListDialogActivity.EditorCallbackListener() {
                    @Override
                    public void onSubmit(List<StocktakingDetailsBean.DataBean.TcheckDeviceListBean> tcheckDeviceListDataBeans) {
                        if (tcheckDeviceList == null)
                            tcheckDeviceList = new ArrayList<TcheckDeviceListBean>();

                        tcheckDeviceList.addAll(tcheckDeviceListDataBeans);
                        initAdapter();
                    }
                }, storeHouseId);
                break;
            case R.id.mTv_Check://核对

                break;
            case R.id.mTv_Submit://提交

                break;
            case R.id.mTv_SaveDraft://保存草稿
                if (!isPush())
                    return;
                onSubmits("1");
                break;
        }
    }

    private boolean isPush() {
        String mStocktakingName = mEt_StocktakingName.getText().toString().trim();
        if (mStocktakingName.isEmpty()) {
            ToastUtils.showToast("请输入盘点名称");
            return false;
        }

        String mStocktakingStaff = mTv_StocktakingStaff.getText().toString().trim();
        if (mStocktakingStaff.isEmpty()) {
            ToastUtils.showToast("请选择盘点人员");
            return false;
        }

        String mStocktakingStoreroom = mTv_StocktakingStoreroom.getText().toString().trim();
        if (mStocktakingStoreroom.isEmpty()) {
            ToastUtils.showToast("请选择库房名称");
            return false;
        }

        if (tcheckDeviceList == null || tcheckDeviceList.size() == 0) {
            ToastUtils.showToast("请添加设备");
            return false;
        }
        return true;
    }

    private void onSubmits(String status) {

        if (tcheckDeviceList != null)
            mDataBean.setTcheckDeviceList(tcheckDeviceList);

        mDataBean.setStatus(status);

        NetworkUtils.getTerminalCheck(new OkGoBackListener(mActivity) {
            @Override
            public void onSuccess(Object body) {
                CommonBean commonBean = (CommonBean) body;
                if (commonBean.getCode() == 200) {
                    ToastUtils.showToast(commonBean.getMsg());
                    onFinish();
                }
            }
        }, mDataBean);
    }

}